package com.admin.controller;


import com.admin.entity.Admin;
import com.admin.entity.Station;
import com.admin.entity.StationRecharge;
import com.admin.service.IStationRechargeService;
import com.admin.service.IStationService;
import com.admin.service.impl.AdminServiceImpl;
import com.admin.service.impl.StationRechargeServiceImpl;
import com.admin.service.impl.StationServiceImpl;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bean.ResponseBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;


/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author marisheng
 * @since 2019-11-21
 */
@RestController
@RequestMapping("/admin/station-recharge")
public class StationRechargeController {
    @Autowired
    private IStationRechargeService rechargeService;
    @Autowired
    private IStationService stationService;

    @Autowired
    private HttpServletRequest request;

    /**
     * 站点的火币充值
     *
     * @param sr
     * @return
     */
    @RequestMapping("/insertRecharge")
    public ResponseBean rechargeById(StationRecharge sr) {
        if (sr.getStationRechargeSum() < 0) {
            return new ResponseBean(500, null, "充值的钱不能为负数");
        }
        //往充值火币的交易记录表新增数据以及修改站点的最大终端数量和用户的火币数量
        int n = rechargeService.rechargeById(sr);
        if (n == 400) {
            return new ResponseBean(400, null, "充值的钱不能大于管理员的");
        }
            return new ResponseBean(200, "", "修改成功");
    }

    /**
     * 查找所有的火币充值交易记录
     *
     * @param page
     * @param limit
     * @param map
     * @return
     */
    @GetMapping("/AllStationRecharge")
    public ResponseBean selectRecharge(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int limit, @RequestParam Map map) {
        Admin admin = (Admin)request.getSession().getAttribute("admin");
        if (admin==null)
        {
            return new ResponseBean(401, "没有登录", null);
        }
        IPage p = rechargeService.selectStationRecharge(page, limit, map);
        return new ResponseBean(0, p.getTotal(), p.getRecords());
    }
}
